<?php
require_once 'config.php';
require_once 'startup.php';     

// Check if user has already logged in, redirect to index page
if($user->isLogged() && isset($cookie->data['token']) && ($cookie->data['token'] == $session->data['token'])){
    $url = $config['url_admin'] . 'index.php'; 
    header('Location: ' .  $url);
    exit();
}    
    
// Render login page
$page_title = 'Administration - Login';
$error = ''; 
if($request->server['REQUEST_METHOD'] == 'POST'){  
    if(isset($request->post['username']) && isset($request->post['password'])){
        if($user->login($request->post['username'], $request->post['password'])){
            $session->data['token'] = md5(rand(0, 15));
            setcookie('token', $session->data['token']);
            
            $url = $config['url_admin'] . 'index.php';                                
            header('Location: ' .  $url);                 
            exit();
        }
        else{
            $error = 'No match for Username and/or Password.';
        }
    }
}

include_once 'header.php';
?>

<body>
    <div id="container">
        <div id="header">
            <div class="div1"><img onclick="location = '<?php echo $config['url_admin'] . 'login.php'; ?>'" title="Administration" src="<?php echo $config['url_admin']; ?>image/logo.png"></div>
        </div>
        
        <div id="content">
            <div style="width: 325px; min-height: 300px; margin-top: 40px; margin-left: auto; margin-right: auto;" class="box">
                <div class="left"></div>
                <div class="right"></div>
                <div class="heading">
                    <h1 style="background-image: url('<?php echo $config['url_admin']; ?>image/lockscreen.png');">Please enter your login details.</h1>
                </div>
                <div style="min-height: 150px;" class="content">
                    <?php if($error){ ?>
                        <div class="warning" style="padding: 3px;"><?php echo $error; ?></div>
                    <?php } ?>
                    <form id="form" enctype="multipart/form-data" method="post" action="<?php echo $config['url_admin'] . basename(__FILE__); ?>">
                        <table style="width: 100%;">
                            <tbody>
                                <tr>
                                    <td rowspan="4" style="text-align: center;"><img alt="Please enter your login details." src="<?php echo $config['url_admin']; ?>image/login.png"></td>
                                </tr>
                                <tr>
                                    <td>
                                        Username:<br>
                                        <input type="text" style="margin-top: 4px;" value="" name="username">
                                        <br>
                                        <br>
                                        Password:<br>
                                        <input type="password" style="margin-top: 4px;" value="" name="password">
                                    </td>
                                </tr>
                                <tr>
                                    <td>&nbsp;</td>
                                </tr>
                                <tr>
                                    <td style="text-align: right;"><a class="button" href="#" onclick="$('#form').submit(); return false;"><span>Login</span></a></td>
                                </tr>
                            </tbody>
                        </table>
                        <input type="hidden" value="<?php echo $config['url_admin']; ?>login.php" name="redirect">
                    </form>  
                </div>
            </div>
        </div>
    </div>

    <script type="text/javascript"><!--
        $('#form input').keydown(function(e) {
            if (e.keyCode == 13) {
                $('#form').submit();
            }
        });
        
        $(document).ready(function(){
            $(".scrollbox").each(function(i) {
                $(this).attr('id', 'scrollbox_' + i);
                sbox = '#' + $(this).attr('id');
                $(this).after('<span><a onclick="$(\'' + sbox + ' :checkbox\').attr(\'checked\', \'checked\');"><Select All> onclick="$(\'' + sbox + ' :checkbox\').attr(\'checked\', \'\');"&gt;&lt;u&gt;Unselect All&lt;/u&gt;&lt;/a&gt;&lt;/span&gt;');
            });
        });
    //-->
    </script> 
                
    <?php include_once 'footer.php'; ?>
</body>
</html>